<?php
/**
 * 
 * @author yuxun
 *
 */

class BossFallService extends Service {
	
	public static function starHeader($params){
		$colortypeList = PubDict::dict($params['g'], 'colortype');  //查询物品品质对应的字典
		$header = array(
				'RecDate'   => array('text' => '日期'),
				'SID'           => array('text' => '游戏服'),
				'BossId'         => array('text' => 'BossID'),
				'BossName'       => array('text' => 'Boss名字'),
				'BossLv'         => array('text' => 'Boss等级'),
				'KillAll'        => array('text' => '击杀次数'),
		);
		
		foreach ($colortypeList as $key=>$val){
			if($key == 0 || $key == 1 || $key == 2 || $key == 3 || $key == 4 || $key == 7){
				continue;
			}
			$QualityData = array(
					'star_0_'.$key => array('text' => '0星'),
					'star_lv_0_'.$key => array('text' => '0星概率'),
					'star_1_'.$key => array('text' => '1星'),
					'star_lv_1_'.$key => array('text' => '1星概率'),
					'star_2_'.$key => array('text' => '2星'),
					'star_lv_2_'.$key => array('text' => '2星概率'),
					'star_3_'.$key => array('text' => '3星'),
					'star_lv_3_'.$key => array('text' => '3星概率'),
			);
			$header['Quality_'.$key] = array('text' => $val, 'include' => $QualityData);
		}
		
		return $header;
	}
	
	public static function starData($params){
		if($params['time_start'] == $params['time_end']){
			$date = $params['time_start'];
		}else{
			$date = date('Y.m.d', strtotime($params['time_start'])).'~'.date('Y.m.d', strtotime($params['time_end']));
		}
	
		$modLog = new ModLog();
		$g_ids = $modLog::getGoodIds('is_analysis', 1);  //查询需要分析的物品
	
		$modBossFall = new ModBossFall();
		$result = $modBossFall->select($params,$g_ids);
	
		$colortypeList = PubDict::dict($params['g'], 'colortype');  //查询物品品质对应的字典
		$new_goods_data = self::itemData($result);
		$new_boss_data = self::bossData($result);
		$new_death_data = self::deathData($params, $result);
	
		$new_data = array();
		foreach($result as $k=>$v){
			$quality = $new_goods_data[$v['ItemId']]['quality'];
			$star = $new_goods_data[$v['ItemId']]['star'];
			
			foreach ($colortypeList as $ck=>$cv){
				if($ck == 0 || $ck == 1 || $ck == 2 || $ck == 3 || $ck == 4 || $ck == 7){
					continue;
				}
				if($star == 0 && $quality == $ck){
					$new_data[$v['BossId']]['star_0_'.$ck] += $v['ItemNum'];
				}
				if($star == 1 && $quality == $ck){
					$new_data[$v['BossId']]['star_1_'.$ck] += $v['ItemNum'];
				}
				if($star == 2 && $quality == $ck){
					$new_data[$v['BossId']]['star_2_'.$ck] += $v['ItemNum'];
				}
				if($star == 3 && $quality == $ck){
					$new_data[$v['BossId']]['star_3_'.$ck] += $v['ItemNum'];
				}
				
			}

			$new_data[$v['BossId']]['RecDate'] = $date;
			$new_data[$v['BossId']]['SID'] = $params['server'];	
			$new_data[$v['BossId']]['BossName'] = $new_boss_data[$v['BossId']]['name'];
			$new_data[$v['BossId']]['BossId'] = $v['BossId'];
			$new_data[$v['BossId']]['BossLv'] = $new_boss_data[$v['BossId']]['lv'];
			$new_data[$v['BossId']]['KillAll'] = $new_death_data[$v['BossId']];
		}	
		
		foreach($new_data as &$v){
			$v['star_lv_0_5'] = $v['star_0_5'] == 0? '0.00%' : '<font color="orange">'.number_format($v['star_0_5'] / $v['KillAll'] * 100, 2, '.', '')."%".'</font>';
			$v['star_lv_1_5'] = $v['star_1_5'] == 0? '0.00%' : '<font color="orange">'.number_format($v['star_1_5'] / $v['KillAll'] * 100, 2, '.', '')."%".'</font>';
			$v['star_lv_2_5'] = $v['star_2_5'] == 0? '0.00%' : '<font color="orange">'.number_format($v['star_2_5'] / $v['KillAll'] * 100, 2, '.', '')."%".'</font>';
			$v['star_lv_3_5'] = $v['star_3_5'] == 0? '0.00%' : '<font color="orange">'.number_format($v['star_3_5'] / $v['KillAll'] * 100, 2, '.', '')."%".'</font>';

			$v['star_0_5'] = $v['star_0_5'] == 0? 0 : '<font color="orange">'.$v['star_0_5'].'</font>';
			$v['star_1_5'] = $v['star_1_5'] == 0? 0 : '<font color="orange">'.$v['star_1_5'].'</font>';
			$v['star_2_5'] = $v['star_2_5'] == 0? 0 : '<font color="orange">'.$v['star_2_5'].'</font>';
			$v['star_3_5'] = $v['star_3_5'] == 0? 0 : '<font color="orange">'.$v['star_3_5'].'</font>';
			
			$v['star_lv_0_6'] = $v['star_0_6'] == 0? '0.00%' : '<font color="red">'.number_format($v['star_0_6'] / $v['KillAll'] * 100, 2, '.', '')."%".'</font>';
			$v['star_lv_1_6'] = $v['star_1_6'] == 0? '0.00%' : '<font color="red">'.number_format($v['star_1_6'] / $v['KillAll'] * 100, 2, '.', '')."%".'</font>';
			$v['star_lv_2_6'] = $v['star_2_6'] == 0? '0.00%' : '<font color="red">'.number_format($v['star_2_6'] / $v['KillAll'] * 100, 2, '.', '')."%".'</font>';
			$v['star_lv_3_6'] = $v['star_3_6'] == 0? '0.00%' : '<font color="red">'.number_format($v['star_3_6'] / $v['KillAll'] * 100, 2, '.', '')."%".'</font>';
			
			$v['star_0_6'] = $v['star_0_6'] == 0? 0 : '<font color="red">'.$v['star_0_6'].'</font>';
			$v['star_1_6'] = $v['star_1_6'] == 0? 0 : '<font color="red">'.$v['star_1_6'].'</font>';
			$v['star_2_6'] = $v['star_2_6'] == 0? 0 : '<font color="red">'.$v['star_2_6'].'</font>';
			$v['star_3_6'] = $v['star_3_6'] == 0? 0 : '<font color="red">'.$v['star_3_6'].'</font>';
		}
		unset($v);
		
		return $new_data;
	}
	
	public static function indexHeader(){
		$header = array(
				'RecDate'       => array('text' => '日期'),
				'SID'           => array('text' => '游戏服'),
				'BossId'         => array('text' => 'BossID'),
				'BossName'       => array('text' => 'Boss名字'),
				'BossLv'         => array('text' => 'Boss等级'),
				'KillAll'        => array('text' => '击杀次数'),
				'ItemId'         => array('text' => '物品ID'),
				'ItemName'       => array('text' => '物品名称'),
				'Quality'        => array('text' => '物品品质'),
				'Star'           => array('text' => '物品星级'),
				'Step'           => array('text' => '物品阶数'),
				'ItemNum'        => array('text' => '产出总量'),
		);		
		return $header;
	}
	

	
	public static function indexData($params){
		if($params['time_start'] == $params['time_end']){
			$date = $params['time_start'];
		}else{
			$date = date('Y.m.d', strtotime($params['time_start'])).'~'.date('Y.m.d', strtotime($params['time_end']));
		}
		
		$modLog = new ModLog();
		$g_ids = $modLog::getGoodIds('is_analysis', 1);  //查询需要分析的物品
		
		$modBossFall = new ModBossFall();
		$result = $modBossFall->page($params,$g_ids);
		
		$colortypeList = PubDict::dict($params['g'], 'colortype');  //查询物品品质对应的字典
		$new_goods_data = self::itemData($result['data']);
		$new_boss_data = self::bossData($result['data']);
		$new_death_data = self::deathData($params, $result['data']);

		foreach($result['data'] as &$v){
			$v['RecDate']           = $date;
			$v['SID']               = $params['server'];
			
			$v['Step'] = $new_goods_data[$v['ItemId']]['stage'];
			$v['Quality'] = $colortypeList[$new_goods_data[$v['ItemId']]['quality']];
			$v['Star'] = $new_goods_data[$v['ItemId']]['star'];
			$color = self::mapColor($new_goods_data[$v['ItemId']]['quality']);
			$v['ItemName'] = '<font color="'.$color.'">'.$new_goods_data[$v['ItemId']]['name'].'</font>';
			
			$v['BossName'] = $new_boss_data[$v['BossId']]['name'];
			$v['BossLv'] = $new_boss_data[$v['BossId']]['lv'];
			
			$v['KillAll'] = $new_death_data[$v['BossId']];
		}
		unset($v);
		return $result;
	}
	
	public static function deathData($params,$data){
		$b_id_array = array();
		foreach ($data as $key=>$val){
			if($val['BossId'] == ''){
				continue;
			}
			$b_id_array[] = $val['BossId'];
		}
		$b_id_array = array_unique($b_id_array);
		$modBossFall = new ModBossFall();
		$death_data= $modBossFall->getInfo($params,$b_id_array);
		
		$new_death_data = array();
		foreach ($death_data as $key=>$val){
			$new_death_data[$val['BossId']] = $val['KillAll'];
		}
		
		return $new_death_data;
	}
	
	/**
	 * 
	 * @param unknown $colortypeList
	 * @param unknown $data
	 * @return unknown
	 */
	public static function itemData($data){
		$g_id_array = array();
		foreach ($data as $key=>$val){
			if($val['ItemId'] == ''){
				continue;
			}
			$g_id_array[] = $val['ItemId'];
		}
		$modGoods = new ModGood();
		$goods_data= $modGoods->getInfo($g_id_array);
		$new_goods_data = array();
		foreach ($goods_data as $key=>$val){
			$new_goods_data[$val['g_id']] = $val;
		}
		
		return $new_goods_data;
	}
	
	/**
	 * 
	 * @param unknown $data
	 */
	public static function bossData($data){
		$b_id_array = array();
		foreach ($data as $key=>$val){
			if($val['BossId'] == ''){
				continue;
			}
			$b_id_array[] = $val['BossId'];
		}
		$modBoss = new ModBoss();
		$boss_data= $modBoss->getInfo($b_id_array);
		$new_boss_data = array();
		foreach ($boss_data as $key=>$val){
			$new_boss_data[$val['b_id']] = $val;
		}
		
		return $new_boss_data;
	}
    
} 